﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using JGSoft.U9CRTAPIEX.DTOs;
using JGSoft.U9CRTAPIEX.DTOs.RequestDTOs;
using JGSoft.U9CRTAPIEX.DTOs.ResultDTOs;
using UFIDA.U9.Base;
using UFIDA.U9.Base.Account;
using UFIDA.U9.Base.Currency;
using UFIDA.U9.Base.SOB;
using UFIDA.U9.CBO.FI.Account;
using UFIDA.U9.CBO.FI.Account.Proxy;
using UFIDA.U9.CBO.FI.BankAccount;
using UFIDA.U9.CBO.FI.DTOs;
using UFIDA.U9.CBO.FI.NaturalAccount;
using UFIDA.U9.CBO.FI.SettlementMethod;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.SCM.Bin;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.CBO.SCM.PropertyTypes;
using UFIDA.U9.GL.TransToGLService;
using UFIDA.U9.GL.TransToGLService.Proxy;
using UFIDA.U9.ISV.PO;
using UFIDA.U9.ISV.PO.Proxy;
using UFIDA.U9.ISV.SM;
using UFIDA.U9.ISV.SM.Proxy;
using UFIDA.U9.ISV.TransferInISV;
using UFIDA.U9.ISV.TransferInISV.Proxy;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Business;
using UFSoft.UBF.MVC;
using UFSoft.UBF.MVC.Common;
using UFSoft.UBF.PL;
using UFSoft.UBF.PL.Engine;
using UFSoft.UBF.Transactions;
using UFSoft.UBF.Util.DataAccess;

namespace JGSoft.U9CRTAPIEX.Controllers
{
    public class Ship2Controller : ApiController
    {

        [HttpPost]
        public ApiResult<List<ResultDTORData>> Create([FromBody] List<ShipRData> datas)
        {
			ApiResult<List<ResultDTORData>> result = new ApiResult<List<ResultDTORData>>();
			List<ResultDTORData> rtndatas = new List<ResultDTORData>();
			try
			{
				List<ShipDTOForIndustryChainData> shipDatas = new List<ShipDTOForIndustryChainData>();
				bool flag = datas == null || datas.Count == 0;
				if (flag)
				{
					ResultDTORData resultBase = new ResultDTORData();
					resultBase.IsSucess = false;
					resultBase.ErrorMsg = "传入业务数据为空，请确认";
					rtndatas.Add(resultBase);
				}
				else
				{
					foreach (ShipRData item in datas)
					{
						ResultDTORData resultDTO = new ResultDTORData();
						ShipDTOForIndustryChainData data = item.ToISVData();
						resultDTO.IsSucess = false;
						resultDTO.OtherID = item.OtherID;
						using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
						{
							{
								try
								{
									List<DocKeyDTOData> svResults = new CreateShipSVProxy
									{
										ShipDTOs = new List<ShipDTOForIndustryChainData>(){	data }
									}.Do();
									scope.Commit();
									resultDTO.IsSucess = true;
									bool flag2 = svResults != null && svResults.Count > 0;
									if (flag2)
									{
										resultDTO.ID = svResults[0].DocID;
										resultDTO.Code = svResults[0].DocNO;
									}
								}
								catch (Exception ex)
								{
									scope.Rollback();
									resultDTO.IsSucess = false;
									resultDTO.ErrorMsg = ex.Message;
								}
								rtndatas.Add(resultDTO);
							}
						}
					}
					

				}
				result.Data = rtndatas;
				return result;
			}
			catch (Exception ex)
			{

				rtndatas.Add(new ResultDTORData { IsSucess = false }); ;
				result.Data = rtndatas;
				return result;
			}

		}

	}
}
